package com.smile.commons.error;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ErrorCodeUtil {

    public static void main(String[] args) {
        System.out.println(ErrorCodeUtil.getErrorDescription(312014));
    }

    private static final Map<Integer, String> ERROR_CODE_MAP = new HashMap<>();

    static {
        // 静态初始化块，在类加载时读取JSON文件并初始化映射
        try {
            // 使用ClassPathResource从resources目录加载文件
            ClassPathResource resource = new ClassPathResource("error.json");
            InputStream inputStream = resource.getInputStream();

            // 使用Jackson解析JSON
            ObjectMapper objectMapper = new ObjectMapper();
            ErrorCodeData errorCodeData = objectMapper.readValue(inputStream, ErrorCodeData.class);

            // 遍历所有错误类别和代码，填充到map中
            for (ErrorCategory category : errorCodeData.getErrors()) {
                for (ErrorCode errorCode : category.getCodes()) {
                    ERROR_CODE_MAP.put(errorCode.getCode(), errorCode.getDescription());
                }
            }

            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("Failed to load error codes from JSON file", e);
        }
    }

    /**
     * 根据错误代码获取错误描述
     * @param code 错误代码
     * @return 错误描述，如果找不到则返回null
     */
    public static String getErrorDescription(int code) {
        return ERROR_CODE_MAP.get(code);
    }

    /**
     * 根据错误代码获取错误描述，如果找不到则返回默认值
     * @param code 错误代码
     * @param defaultValue 默认值
     * @return 错误描述或默认值
     */
    public static String getErrorDescription(int code, String defaultValue) {
        return ERROR_CODE_MAP.getOrDefault(code, defaultValue);
    }

    // 内部类用于JSON解析
    private static class ErrorCodeData {
        private List<ErrorCategory> errors;

        public List<ErrorCategory> getErrors() {
            return errors;
        }

        public void setErrors(List<ErrorCategory> errors) {
            this.errors = errors;
        }
    }

    private static class ErrorCategory {
        private String category;
        private List<ErrorCode> codes;

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public List<ErrorCode> getCodes() {
            return codes;
        }

        public void setCodes(List<ErrorCode> codes) {
            this.codes = codes;
        }
    }

    private static class ErrorCode {
        private int code;
        private String description;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}